import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
} from '@nestjs/common';
import { DictService } from './dict.service';
import { CreateDictDto } from './dto/create-dict.dto';
import { UpdateDictDto } from './dto/update-dict.dto';
import { PageQuery } from '../../../util/entity/page.entity';
import { Dict } from './entities/dict.entity';
import { HasRoles, HasPerms } from '../../../common/decorator/my.decorator';
import { Perm } from '../../../util/enum/perm.enum';
import { RoleEnum } from '../../../util/enum/role.enum';

@Controller()
export class DictController {
  constructor(private readonly dictService: DictService) {}

  /**
   * 字典列表
   * @param pageQuery
   */
  @Post('list')
  findAll(@Body() pageQuery: PageQuery<Dict>) {
    return this.dictService.findAll(pageQuery);
  }

  /**
   * 创建字典
   * @param createDictDto
   */
  @HasPerms(Perm.SysDictAdd)
  @Post()
  create(@Body() createDictDto: CreateDictDto) {
    return this.dictService.create(createDictDto);
  }

  /**
   * 通过code查找孩子
   * @param code
   */
  @Get('code/:code')
  findDictSonByCode(@Param('code') code: string) {
    return this.dictService.findDictSonByCode(code);
  }

  @HasPerms(Perm.SysDictEdit)
  @Get(':id')
  findOne(@Param('id') id: number) {
    return this.dictService.findOne(+id);
  }

  @HasPerms(Perm.SysDictEdit)
  @Patch(':id')
  update(@Param('id') id: number, @Body() updateDictDto: UpdateDictDto) {
    return this.dictService.update(id, updateDictDto);
  }

  @HasRoles(RoleEnum.Root)
  @HasPerms(Perm.SysDictDelete)
  @Delete(':id')
  async remove(@Param('id') id: number) {
    await this.dictService.remove(id);
  }
}
